package com.xdjk.networkdemo.net;

import android.os.Environment;

import com.orhanobut.logger.Logger;
import com.xdjk.networkdemo.entity.LoginParam;
import com.xdjk.networkdemo.entity.Principal;
import com.xdjk.networkdemo.net.bean.BaseResponse;
import com.xdjk.networkdemo.net.bean.WeatherData;
import com.xdjk.networkdemo.net.bean.DownInfo;
import com.xdjk.networkdemo.net.bean.DownState;
import com.xdjk.networkdemo.net.db.DBUtils;
import com.xdjk.networkdemo.utils.MediaTypes;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by zhanghs on 2017/8/16.
 */

public class UserinfoServer {

    public static MultipartBody filesToMultipartBody(List<File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();

        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
            builder.addFormDataPart("file", file.getName(), requestBody);
        }
        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }

    public static List<MultipartBody.Part> filesToMultipartBodyParts(List<File> files) {
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
            parts.add(part);
        }
        return parts;
    }

    //上传头像
    //参数是，图片路径，描述，和subscriber。
    public static void uploadUserIcon(String imgpath, String des, UploadOCallBack<String> uploadCallbacks) {
        Logger.d("filePath=" + imgpath);
        //根据图片路径构建图片的RequestBody
        File file = new File(imgpath);
        ProgressRequestBody progressRequestBody = new ProgressRequestBody(file, MediaType.parse("image/*"), uploadCallbacks);

        HttpManager.getInstance().builder(HttpManager.BASE_URL3).doHttpRequest(HttpManager.getInstance().getHttpService().uploadUserFile(des, progressRequestBody), uploadCallbacks);
    }

    public static void getWeatherData(String cityCode,Subscriber<WeatherData> subscriber) {
        HttpManager.getInstance().builder().doHttpRequest(HttpManager.getInstance().getHttpService().getWeatherData(cityCode), subscriber);
    }

    public static void getWeatherDataByUrl(String cityCode, Subscriber<WeatherData> subscriber) {
        HttpManager.getInstance().builder(HttpManager.BASE_URL2).doHttpRequest(HttpManager.getInstance().getHttpService().getWeatherDataByCode(cityCode), subscriber);
    }
    //上传文件
    //参数是，图片路径，描述，和subscriber。
    public static void uploadFileWithRequestBody(String imgpath, String des, UploadOCallBack<ResponseBody> subscriber) {
        File file = new File(imgpath);
        if (file.exists()) {
            Logger.e("file=" + file.getName());
            ProgressRequestBody progressRequestBody = new ProgressRequestBody(file, MediaTypes.APK, subscriber);
            //参数1 数组名，参数2 文件名。
            MultipartBody.Part file1Part = MultipartBody.Part.createFormData("file", file.getName(), progressRequestBody);
            MultipartBody.Part textPart = MultipartBody.Part.createFormData("desc", "啦啦111000啦啦");
            HttpManager.getInstance().builder(HttpManager.BASE_URL3).doHttpRequest(HttpManager.getInstance().getHttpService().uploadFile(file1Part,textPart), subscriber);
        } else {
            Logger.e("文件不存在");
        }
    }
    //上传文件
    public static void fileDownload(String url, DownloadCallBack<ResponseBody> callBack) {
        url = "http://172.20.191.212:8081/download/test";
        File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "test" + ".apk");
        DownInfo apkApi=new DownInfo(url);
        apkApi.setState(DownState.START);
        apkApi.setDownloadCallBack(callBack);
        apkApi.setSavePath(outputFile.getAbsolutePath());
        DBUtils.getInstance().save(apkApi);
        HttpDownManager.getInstance().startDown(apkApi);
    }

    public static void loginTest(Subscriber<BaseResponse<Principal>> subscriber) {
//        1
        HttpManager.getInstance().builder(HttpManager.BASE_URL3).doHttpRequest(HttpManager.getInstance().getHttpService().loginTest("thinkgem", "admin", "true", false), subscriber);
//        2
//        Map<String, String> map = new HashMap<>();
//        map.put("username", "thinkgem");
//        map.put("password", "admin");
//        map.put("mobileLogin", "true");
//        map.put("rememberMe", "false");
//        HttpManager.getInstance().builder(HttpManager.BASE_URL3).doHttpRequest(HttpManager.getInstance().getHttpService().loginTest2(map), subscriber);
    }
}
